package com.xltt.vehicleControl.presenter;

import android.util.Log;

import com.xltt.vehicleControl.base.BasePresenter;
import com.xltt.vehicleControl.bean.BaseData;
import com.xltt.vehicleControl.bean.BaseResult;
import com.xltt.vehicleControl.bean.DiagnosticsResponse;
import com.xltt.vehicleControl.bean.PushConfig;
import com.xltt.vehicleControl.bean.RemoteControlRequest;
import com.xltt.vehicleControl.bean.RemoteControlResponse;
import com.xltt.vehicleControl.bean.TcVehicleProfileResponse;
import com.xltt.vehicleControl.bean.TscVehicleCapabilityResponse;
import com.xltt.vehicleControl.bean.UpdateSession;
import com.xltt.vehicleControl.bean.VehicleStatusResponse;
import com.xltt.vehicleControl.contract.IVehicleControlContract;
import com.xltt.vehicleControl.model.UserModel;
import com.xltt.vehicleControl.model.VehicleControlModel;
import com.xltt.vehicleControl.util.Logger;
import com.xltt.vehicleControl.util.RxJavaUtil;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;



public class VehicleControlPresenter extends BasePresenter<IVehicleControlContract.IVehicleControlView> implements IVehicleControlContract.IVehicleControlPresenter {

    private static final String TAG = VehicleControlPresenter.class.getName();
    private VehicleControlModel mVehicleControlModel;
    private UserModel mUserModel;
    private BaseResult<TcVehicleProfileResponse> mTcVehicleProfileResponse;
    private BaseResult<RemoteControlResponse> mRemoteControlResponse;
    private BaseResult<VehicleStatusResponse> mVehicleStatusResponse;
    private BaseResult<TscVehicleCapabilityResponse> mTscVehicleCapabilityResponse;
    private BaseResult<BaseData> mBaseData;
    private PushConfig mPushConfig;

    private VehicleControlPresenter() {
        mVehicleControlModel = VehicleControlModel.getInstance();
        mUserModel = UserModel.getInstance();
    }

    public static VehicleControlPresenter getInstance() {
        return VehicleControlPresenter.Inner.instance;
    }


    @Override
    public void getVehicleList(String userId, String type) {
        Observable observable = mVehicleControlModel.getVehicleList(userId,type).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }

        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<TcVehicleProfileResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<TcVehicleProfileResponse> poetryEntity) {
                mTcVehicleProfileResponse = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mTcVehicleProfileResponse != null) {
                    getMvpView().getVehicleListResult(mTcVehicleProfileResponse);
                }
            }
        });

    }

    @Override
    public void remoteControl(String var1, RemoteControlRequest var2) {
        Observable observable = mVehicleControlModel.remoteControl(var1,var2).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }


        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<RemoteControlResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<RemoteControlResponse> poetryEntity) {
                mRemoteControlResponse = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mRemoteControlResponse != null) {
                    getMvpView().remoteControlResult(mRemoteControlResponse);
                }
            }
        });
    }

    @Override
    public void getVehicleStatus(String var1, String var2, String var3, String var4) {
        Observable observable = mVehicleControlModel.getVehicleStatus(var1,var2,var3,var4).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<VehicleStatusResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<VehicleStatusResponse> poetryEntity) {
                mVehicleStatusResponse = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mVehicleStatusResponse != null) {
                    getMvpView().getVehicleStatusResult(mVehicleStatusResponse);
                }
            }
        });
    }

    @Override
    public void getVehicleCapability(String vin, int pageSize, int pageIndex, int vehicleType, String sortField, String direction) {
        Observable observable = mVehicleControlModel.getVehicleCapability(vin,pageSize,pageIndex,vehicleType,sortField,direction).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<TscVehicleCapabilityResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<TscVehicleCapabilityResponse> poetryEntity) {
                mTscVehicleCapabilityResponse = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mTscVehicleCapabilityResponse != null) {
                    getMvpView().getVehicleCapabilityResult(mTscVehicleCapabilityResponse);
                }
            }
        });
    }

    @Override
    public void logout() {
        Observable observable = mUserModel.logout().doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<BaseData>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResult<BaseData> poetryEntity) {
                mBaseData = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mBaseData != null) {
                    getMvpView().logoutResult(mBaseData);
                }
            }
        });
    }

    @Override
    public void deviceConfig(String deviceId, String appId, String gapp, String appType, String aliPushId, String deviceClass, String modelCode, String pushType) {
        Observable observable = mVehicleControlModel.deviceConfig(deviceId,appId,gapp,appType,aliPushId,deviceClass,modelCode,pushType).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<PushConfig>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(PushConfig poetryEntity) {
                mPushConfig = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mPushConfig != null) {
                    getMvpView().deviceConfigResult(mPushConfig);
                }
            }
        });
    }

    @Override
    public void update(String vin, UpdateSession updateSession) {
        Observable observable = mVehicleControlModel.update(vin,updateSession).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<BaseData>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<BaseData> poetryEntity) {
                mBaseData = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mBaseData != null) {
                    getMvpView().updateResult(mBaseData);
                }
            }
        });
    }

    private static class Inner {
        private static final VehicleControlPresenter instance = new VehicleControlPresenter();
    }

}
